home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / prs2 / prs2delete.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.3 KB  |  163 lines

  1. /*
  2.  * FILE:
  3.  *   prs2delete.c
  4.  *
  5.  * IDENTIFICATION:
  6.  *   $Header: /private/postgres/src/rules/prs2/RCS/prs2delete.c,v 1.12 1991/05/09 18:10:59 sp Exp $
  7.  *
  8.  * DESCRIPTION:
  9.  *   The main routine is prs2Delete() which is called whenever
  10.  *   a delete operation is performed by the executor.
  11.  *
  12.  */
  13.  
  14.  
  15.  
  16. #include "tmp/c.h"
  17. #include "access/htup.h"
  18. #include "storage/buf.h"
  19. #include "utils/rel.h"
  20. #include "rules/prs2.h"
  21. #include "rules/prs2stub.h"
  22. #include "nodes/execnodes.h"    /* which includes access/rulescan.h */
  23. #include "parser/parse.h"    /* for the DELETE */
  24.  
  25.  
  26. /*------------------------------------------------------------------
  27.  *
  28.  * prs2Delete()
  29.  *
  30.  * Activate any rules that apply to the deleted tuple
  31.  *
  32.  * Arguments:
  33.  *      prs2EStateInfo: loop detection information stored in the EState
  34.  *    tuple: the tuple to be deleted
  35.  *    buffer:    the buffer for 'tuple'
  36.  *    relation: the relation where 'tuple' belongs
  37.  * Return value:
  38.  *    PRS2MGR_INSTEAD: do not perform the delete
  39.  *      PRS2MGR_TUPLE_UNCHANGED: Ok, go ahead & delete the tuple
  40.  *------------------------------------------------------------------
  41.  */
  42. Prs2Status
  43. prs2Delete(prs2EStateInfo, relationRuleInfo, explainRelation, tuple, buffer,
  44.             rawTuple, rawBuffer, relation)
  45. Prs2EStateInfo prs2EStateInfo;
  46. RelationRuleInfo relationRuleInfo;
  47. Relation explainRelation;
  48. HeapTuple tuple;
  49. Buffer buffer;
  50. HeapTuple rawTuple;
  51. Buffer rawBuffer;
  52. Relation relation;
  53. {
  54.  
  55.     RuleLock locks, l1, l2;
  56.     AttributeValues attrValues;
  57.     bool insteadRuleFound;
  58.     TupleDescriptor tupDesc;
  59.     int i;
  60.  
  61.     /*
  62.      * Find the locks of the tuple.
  63.      *
  64.      * NOTE: the 'rawTuple' is the tuple as retrieved
  65.      * from the disk, so it has all required lock information in
  66.      * it (as opposed to tuples formed when executing the target
  67.      * lists of the various executor nodes).
  68.      * On the other hand, 'tuple' is a result of projections,
  69.      * etc, identical to 'rawTuple' with only 2 differences:
  70.      * a) no rule locks
  71.      * b) all "on retrieve" backward chaining rules have been activated
  72.      */
  73.     tupDesc = RelationGetTupleDescriptor(relation);
  74.  
  75.     if (relationRuleInfo->ignoreTupleLocks)
  76.     l1 = prs2MakeLocks();
  77.     else
  78.     l1 = prs2GetLocksFromTuple (rawTuple, rawBuffer);
  79.     l2 = relationRuleInfo->relationLocks;
  80.     locks = prs2LockUnion(l1, l2);
  81.     prs2FreeLocks(l1);
  82.  
  83.     /*
  84.      * if there are no rules, then return immediatelly
  85.      */
  86.     if (locks == NULL || prs2GetNumberOfLocks(locks) == 0) {
  87.     return(PRS2_STATUS_TUPLE_UNCHANGED);
  88.     }
  89.  
  90.     /*
  91.      * Ooops! There is a problem here... 'tuple' is set to 'nil'
  92.      * by the executor, so for the time being use the 'rawTuple'
  93.      * (the result is that we might have to re-evaluate some backward
  94.      * chaining rules...)
  95.      */
  96.     tuple = rawTuple;
  97.     buffer = rawBuffer;
  98.  
  99.     /*
  100.      * now extract from the tuple the array of the attribute values.
  101.      */
  102.     attrValues = attributeValuesCreate(tuple, buffer, relation);
  103.  
  104.     /*
  105.      * activate all 'on delete' rules...
  106.      * NOTE: all these rules must have an attribute no equal to
  107.      * `InvalidAttributeNumber' because they do not refer to any
  108.      * field in particular....
  109.      */
  110.     prs2ActivateForwardChainingRules(
  111.         prs2EStateInfo,
  112.         explainRelation,
  113.         relation,
  114.         InvalidAttributeNumber,
  115.         LockTypeDeleteAction,
  116.         PRS2_OLD_TUPLE,
  117.         tuple->t_oid,
  118.         attrValues,
  119.         locks,
  120.         LockTypeRetrieveWrite,
  121.         InvalidObjectId,
  122.         InvalidAttributeValues,
  123.         InvalidRuleLock,
  124.         LockTypeInvalid,
  125.         &insteadRuleFound,
  126.         (AttributeNumberPtr) NULL,
  127.         (AttributeNumber) 0);
  128.  
  129.     /*
  130.      * if this tuple contains any 'export' locks, then
  131.      * activate the appropriate rule plans...
  132.      */
  133.     for (i=0; i<prs2GetNumberOfLocks(locks); i++) {
  134.     Prs2OneLock oneLock;
  135.     AttributeNumber attrno;
  136.  
  137.     oneLock = prs2GetOneLockFromLocks(locks, i);
  138.     if (prs2OneLockGetLockType(oneLock)  == LockTypeExport) {
  139.         attrno = prs2OneLockGetAttributeNumber(oneLock);
  140.         if (!attrValues[attrno-1].isNull)
  141.         prs2ActivateExportLockRulePlan(oneLock,
  142.                 attrValues[attrno-1].value,
  143.                 tupDesc->data[attrno-1]->atttypid,
  144.                 DELETE);
  145.     }
  146.     }
  147.     
  148.     /*
  149.      * free allocated stuff...
  150.      */
  151.     prs2FreeLocks(locks);
  152.     attributeValuesFree(attrValues, relation);
  153.  
  154.     /*
  155.      * Was this rule an 'instead' rule ???
  156.      */
  157.     if (insteadRuleFound) {
  158.     return(PRS2_STATUS_INSTEAD);
  159.     } else {
  160.     return(PRS2_STATUS_TUPLE_UNCHANGED);
  161.     }
  162. }
  163.